﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace FunctionGraph
{
    public class Instruction
    {
        public MathOperator OpCode;
        public int Operand1, Operand2, Result; // registers
        public Instruction(MathOperator opCode, int rs, int op1, int op2 = -1)
        {
            OpCode = opCode;
            Operand1 = op1;
            Operand2 = op2;
            Result = rs;
        }
        public override string ToString()
        {
            return string.Format("{0}\t{3}, {1}, {2}", Enum.GetName(typeof(MathOperator), OpCode), Operand1, Operand2, Result);
        }
    }

    public class Executable
    {
        public int RegisterCount = 0;
        public List<double> Constants = new List<double>();
        public List<Instruction> Instructions = new List<Instruction>();
        public void Serialize(BinaryWriter writer)
        {
            writer.Write(RegisterCount);
            writer.Write(Constants.Count);
            foreach (var c in Constants)
            {
                writer.Write(c);
            }
            writer.Write(Instructions.Count);
            foreach (var instr in Instructions)
            {
                writer.Write((byte)instr.OpCode);
                writer.Write(instr.Operand1);
                writer.Write(instr.Operand2);
                writer.Write(instr.Result);
            }
        }
    }
}
